<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['diy']=$this->plugin_setting('diy');
$basic=$this->plugin_setting('basic');
$return['basic']=$basic;
$return['banner']=$basic['image'];
if($_RQ['op']=='main'){
	if(!$_RQ['id']){
		exi('没有找到该门店，请重新选择！','error');
	}
	//print_r($_RQ);exit;
	//其实要展示的信息为课程信息
	$lesson=pdo_get('jy_bdd_lesson',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['lessonid']));
	$store=pdo_get('jy_bdd_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//查询本项目可服务员工(未锁定，状态为可服务)
	$worker_list=pdo_getall('jy_bdd_worker',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1','lessonid'=>$_RQ['lessonid'],'storeid'=>$_RQ['id']));
	//查询本项目的访问人数及添加访问人数
	/*
	$num=pdo_getcolumn('jy_bdd_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'num');
	$serve['num']=$num+1;
	pdo_update('jy_bdd_serve',array('num'=>$serve['num']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//已支付，已确认，已完成订单数
	$num1=pdo_count('jy_bdd_order',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$_RQ['id'],'status'=>'1'));
	$num2=pdo_count('jy_bdd_order',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$_RQ['id'],'status'=>'2'));
	$num3=pdo_count('jy_bdd_order',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$_RQ['id'],'status'=>'4'));
	$serve['ordernum']=$num1+$num2+$num3;
	$serve_time=json_decode($serve['serve_time'],true);
	*/
	//日期 今天，未来七天
	$data=array();
	$data['0']['riqi']=date("m-d",time("day")); 
	$data['1']['riqi']=date("m-d",strtotime("+1 day")); 
	$data['2']['riqi']=date("m-d",strtotime("+2 day")); 
	$data['3']['riqi']=date("m-d",strtotime("+3 day")); 
	$data['4']['riqi']=date("m-d",strtotime("+4 day")); 
	$data['5']['riqi']=date("m-d",strtotime("+5 day")); 
	$data['6']['riqi']=date("m-d",strtotime("+6 day")); 
	$weekarray=array("日","一","二","三","四","五","六"); //先定义一个数组
	$data['0']['xinq']= "周".$weekarray[date("w")];
	$data['1']['xinq']= "周".$weekarray[date("w",strtotime(date("Y-m-d",strtotime("+1 day"))))];
	$data['2']['xinq']= "周".$weekarray[date("w",strtotime(date("Y-m-d",strtotime("+2 day"))))];
	$data['3']['xinq']= "周".$weekarray[date("w",strtotime(date("Y-m-d",strtotime("+3 day"))))];
	$data['4']['xinq']= "周".$weekarray[date("w",strtotime(date("Y-m-d",strtotime("+4 day"))))];
	$data['5']['xinq']= "周".$weekarray[date("w",strtotime(date("Y-m-d",strtotime("+5 day"))))];
	$data['6']['xinq']= "周".$weekarray[date("w",strtotime(date("Y-m-d",strtotime("+6 day"))))];
	
	//$this->session('serve_id',$_RQ['id']);
	
	//获取当前时间
	/*
	$time=time();
	foreach($serve_time as $k=>&$item){
		$num=pdo_count('jy_bdd_order',array('timeid'=>$k,'uniacid'=>$_SESSION['uniacid'],'day'=>date("Y-m-d",time("day")),'serve_id'=>$_RQ['id']));
		
		if($item['yuyue']>$num){
		$item['yuyue']=$item['yuyue']-$num;
		}
		else{
		$item['yuyue']=0;
		}
		//时间转为时间戳
		$item['shijian']=strtotime(date("Y-m-d",time("day")).$item['times']['start']);
		if($item['shijian']<time()){
			$item['show']=0;
		}
		else{
			$item['show']=1;
		}
	}
	*/
	//查询当日课程
	$store_lesson=pdo_getcolumn('jy_bdd_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'store_lesson');
	$store_lesson=json_decode($store_lesson,true);
	//查询当日课程
	if($store_lesson){
		$list=array();
		foreach($store_lesson as $item){
			if($item['xinqi']==$data['0']['xinq']){
				$list[]=$item;
			}
		}
		if($list){
			foreach($list as &$row){
				$row['worker']=pdo_get('jy_bdd_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$row['workerid']));
				$row['lesson']=pdo_get('jy_bdd_lesson',array('uniacid'=>$_SESSION['uniacid'],'id'=>$row['lessonid']));
			//	$row['time']=json_decode($row['time'],true);
			}
		}
	}
	$return['store']=$store;
	$return['lessons']=$list;
	//$return['package']=json_decode($package);
	$return['worker_list']=$worker_list;
	$return['province']=file_get_contents('core/resource/area.txt');
	$return['province']=unserialize($return['province']);
	$return['data']=$data;
	$return['post']=array('lessonid'=>$_RQ['lessonid'],'storeid'=>$_RQ['id']);
	//print_r($fileds);exit;
	exi($return);
}
if($_RQ['op']=='notice'){
	$postdata=get_postdata();
	//日期 今天，未来七天
	$data=array();
	$data['0']=date("Y-m-d",time("day")); 
	$data['1']=date("Y-m-d",strtotime("+1 day")); 
	$data['2']=date("Y-m-d",strtotime("+2 day")); 
	$data['3']=date("Y-m-d",strtotime("+3 day")); 
	$data['4']=date("Y-m-d",strtotime("+4 day")); 
	$data['5']=date("Y-m-d",strtotime("+5 day")); 
	$data['6']=date("Y-m-d",strtotime("+6 day"));
	if($postdata){
		if($postdata['worker_id']==''){
			exi('请选择服务人员','error');
		}
		//exi($postdata['timeid'],'error');
			if(!$postdata['username']){
			exi('请输入联系人姓名','error');
		}
		if(!$postdata['mobile']){
			exi('请输入联系人电话','error');
		}
		if(!preg_match("/^1[345678]{1}\d{9}$/",$postdata['mobile'])){  
	 
			exi('请输入正确的手机号码','error');
		} 
		if($postdata['fileds']){
			foreach ($postdata['fileds'] as $item) {
				if($item['ismust']==1 && empty($item['data'])){
					exi('请完善'.$item['title'].'内容后再提交','error');
				}
			}
		}
			$price=pdo_getcolumn('jy_bdd_lesson',array('id'=>$postdata['lessonid']),'price');
			//$time=ijson_encode(array('data'=>$postdata['data'],'start'=>$postdata['start'],'end'=>$postdata['end']));	
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'storeid'=>	$postdata['storeid'],
				'lesson_id'=>$postdata['lessonid'],
				'worker_id'=>$postdata['worker_id'],
				'money'=>$price,
				'day'=>$data[$postdata['dayid']],
				'username'=>$postdata['username'],
				'mobile'=>$postdata['mobile'],
				'remark'=>$postdata['remark'],
				'ordersn'=>get_ordersn('jy_bdd_order','ordersn'),
				'createtime'=>TIMESTAMP,
			);
			pdo_insert('jy_bdd_order',$insert);
			$id=pdo_insertid();
	}
	exi('','','jy_bdd/confirm/main/id='.$id);
}
if($_RQ['op']=='get_lesson'){
	
	if(!isset($_RQ['storeid'])){
		exi('未获取门店信息','error');
	}
	if(!isset($_RQ['dayid'])){
		exi('请重新选择日期','error');
	}
	//print_r($_RQ['xinqi'].'yy');exit;
	$store_lesson=pdo_getcolumn('jy_bdd_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['storeid']),'store_lesson');
	$store_lesson=json_decode($store_lesson,true);
	//查询当日课程
	if($store_lesson){
		$list=array();
		foreach($store_lesson as $item){
			if($item['xinqi']==$_RQ['xinqi']){
				$list[]=$item;
			}
		}
		if($list){
			foreach($list as &$row){
				$row['worker']=pdo_get('jy_bdd_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$row['workerid']));
				$row['lesson']=pdo_get('jy_bdd_lesson',array('uniacid'=>$_SESSION['uniacid'],'id'=>$row['lessonid']));
			//	$row['time']=json_decode($row['time'],true);
			}
		}
	}
	
	
	//print_r($list);exit;
	$return['list']=$list;
	$return['success']=1;
	
	exi($return);
}
if($_RQ['op']=='package'){
	if(!isset($_RQ['id'])){
		exi('请重新选择项目','error');
	}
	//exi($_RQ['id'],'error');
	$package=pdo_getcolumn('jy_bdd_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'package');
	$package=json_decode($package,true);
	//查询自定义字段
	$fileds=pdo_getall('jy_bdd_fileds',array('uniacid'=>$_SESSION['uniacid'],'serve_id'=>$_RQ['id']));
	foreach($fileds as &$item){
		$item['value']=explode(';',$item['value']);
	}
	$return['post']=array();
	$return['post']['fileds']=$fileds;
	$return['package']=$package;
	$return['success']=1;
	exi($return);
}
